<?php
/**
 * 商品内容页控制器
 */
class ContentController extends CommonController{
	//前台内容首页
	public function index(){
		header("Content-type:text/html;charset=utf-8");
		//获取商品id
		if(isset($_GET['gid']) && $_GET['gid'] !== 0){
			$gid = Q('get.gid');
		}else{
			$gid = 0;
//			$this->error('访问商品不从存在',__ROOT__.'/404.html');
		}
		//查询商品信息 分配视图信息
		$goodsData = M()->JOIN("__goods__ g JOIN __goods_info__ info  ON g.gid = info.goods_gid ")->where("gid = '{$gid}'")->find();

		//组合图片路径
		$goodsImg = array_filter(explode(',', $goodsData['goods_info_smallIMg']));
		foreach($goodsImg as $k => $v ){
			$goodsData['goodsImg'][$k]['bigImg']  = __ROOT__.'/upload/goods/big/'.$v.'_thumb_800X800.jpg';
			$goodsData['goodsImg'][$k]['midImg']  = __ROOT__.'/upload/goods/middle/'.$v.'_thumb_400X400.jpg';
			$goodsData['goodsImg'][$k]['smaImg']  = __ROOT__.'/upload/goods/small/'.$v.'_thumb_100X100.jpg';
		}
		//相关栏目 
		$cid = $goodsData['category_cid'];
		$category = M('category')->where("cid = '{$cid}'")->find();
		$pid = $category['pid'];
		//查找父级栏目
		$category = M('category')->where("pid = '{$pid}'")->all();
		//循环组合父级栏目的子栏目
		foreach($category as $k =>$v ){
			//用$v的cid 查询栏目下所有pid和cid 相同的子栏目
			$cid = $v['cid'];
			$Soncategory = M('category')->where("pid = '{$cid}'")->all();
			//判断是否有子栏目 如果有就查询子栏目  没有就查询父级栏目 再组合子栏目
			if(empty($Soncategory)){
				$pid = $v['pid'];									
				$Soncategory = M('category')->where("pid = '{$pid}'")->all();
			}
			//判断返回是否存在栏目
			if($Soncategory){
				foreach($Soncategory as $kk => $vv){
					$category[$k]['son']['category_name'][$kk] = $vv['category_name'];
					$category[$k]['son']['cid'][$kk] = $vv['cid'];
				}

			}else{//有子栏目就组合数据  没有就设置空
				$category[$k] =null;
			}

			
		}
		$this->assign('category',$category);
		//品牌推荐
		$goodsData['brand'] = M("brand")->where("brand_hot = 1")->limit(20)->all();
		//热门商品推荐  查询信息
		 $goodsHot = M()->JOIN("__goods__ g JOIN __goods_info__ info ON g.gid = info.goods_gid")->where('goods_hot = 1')->limit(5)->all();
		 //组合一张图片路径
		 foreach($goodsHot as $k => $v){
//			$goodsHot[$k]['imgFilename'] = array_filter(explode(',', $v['goods_info_smallIMg']));
			$imgStr = ltrim($v['goods_info_smallIMg'],',');
			$imgStr = strchr($imgStr, ',',true);
//			echo substr(ltrim($v['goods_info_smallIMg'],','),0,strchr(ltrim($v['goods_info_smallIMg'],','), ','));die;
		    $goodsHot[$k]['imgUrl'] = __ROOT__.'/upload/goods/middle/'.$imgStr.'_thumb_400X400.jpg';
		 }
		//查询商品属性 返回数组
		$GoodsAttrGroup = M()->JOIN("__goods_attr__ attr join __type_attr__ type  on  type.aid = attr.type_attr_aid  ")->where("goods_gid ='{$gid}' and attr_class =1")->group("type_attr_aid")->order("type_attr_aid asc")->all();//分组
		$GoodsAttrInfo = M()->JOIN("__goods_attr__ attr join __type_attr__ type  on  type.aid = attr.type_attr_aid  ")->where("goods_gid ='{$gid}' and attr_class =1")->all();
		foreach($GoodsAttrGroup as $k =>$v ){
			foreach($GoodsAttrInfo as $kk =>$vv){
				if($vv['aid'] == $v['aid']){
					$GoodsAttrGroup[$k]['attrValue'][] = $vv['goods_attr_value'];
					$GoodsAttrGroup[$k]['attrId'][] = $vv['goods_attr_id'];
				}
			}
		}
		
		//查询咨询信息分配
//		$test = M()->JOIN("__user__ u1 join __user__ u2 on u1.uid=u2.uid")->field("u1.uid,u2.uid")->where("u1.uid=1")->all();var_dump($test);
//		$questionInfo = M()->JOIN("__user__ u1 join __user__ u2 on u1.uid=u2.uid")->getField("u1.uid,u2.uid,",true);p($questionInfo);
		$questionInfo = M()->JOIN("__user__ ua join __user__ uq join __question__ q on q.question_question_uid=uq.uid and q.question_answer_uid=ua.uid")->field("qid,ua.username as uaname,uq.username as uqname,q.question_con,q.question_answer,q.question_addtime")->where("goods_gid='{$gid}'")->all();
		$this->assign('questionInfo',$questionInfo);

		//写入点击量
		$data['goods_click'] = $goodsData['goods_click'] +1;
		M('goods')->where("gid ='{$gid}'")->update($data);
		
		//评论信息
		$comment = M()->JOIN("__comment__ c join __goods__ g join __user__ u  on g.gid=c.goods_gid and c.user_uid=u.uid")->where("gid='{$gid}'")->all();
		$this->assign('comment',$comment);
		$this->assign('GoodsAttrGroup',$GoodsAttrGroup);
		$this->assign('GoodsAttrInfo',$GoodsAttrInfo);
//		 //将组合数据分配至视图
		$this->assign('goodsHot',$goodsHot);
		$this->assign('goodsData',$goodsData); //分配视图 商品信息  		
		$this->dis('content');
	}

	//商品规格属性异步
	public function getAttr(){
		if(IS_AJAX){
			//获取psot 属性id 
			$attr = Q('post.attr');
			$gid = Q('post.gid');
			//取出右边多余‘，’号
			$attr = rtrim($attr,',');
			$attr = str_replace(' ', '', $attr);
			//查询数据库
			$attrInfo = M('goods_list')->where("goods_list_compositeAttrId ='{$attr}' and goods_gid = '{$gid}'")->find();
			if(count($attrInfo)>=1){
				$attrInfo['error'] = 0;
			}else{
				$attrInfo['error'] = 1;
			}
			$this->ajax($attrInfo);
		}		
	}
	
	//验证码
	public function code(){
		$code = new code(60,30,'#ffffff','#fff',3,12);
		$code->show();
	}
	
	//检查验证码
	public function cheackCode(){
		$code = Q('post.code');
		//验证
		if(strtoupper($code) == $_SESSION['code']){
			$data['code'] = '1';
		}else{
			$data['code'] ='';
		}
		$this->ajax($data);
	}
	
	//商品规格属性异步 查询单个属性确定其他属性是否可选
	public function getAttrFirst(){
		if(IS_AJAX){
			//获取psot 属性id 
			$attr = Q('post.attr');
			$gid = Q('post.gid');
			//取出右边多余‘，’号
			$attr = rtrim($attr);
			$attr = str_replace(' ', '', $attr);
			//查询数据库
			$attrInfo = M('goods_list')->where("goods_gid = '{$gid}'")->all();
			//判断如果查询有值  就遍历 比对相同项抓取
			foreach($attrInfo as $k => $v){
				$bool = strstr($v['goods_list_compositeAttrId'],$attr);
				if($bool != false){
					$data[$k]['goods_list_id'] =$v['goods_list_id'];
		            $data[$k]['goods_list_nums'] = $v['goods_list_nums'];
		            $data[$k]['goods_list_stock'] = $v['goods_list_stock'];
		            $data[$k]['goods_gid'] = $v['goods_gid'];
					//把传过来的attr Id 去除掉 只留另外一个属性的id 待js判断 处理
					$v['goods_list_compositeAttrId'] = str_replace(array($attr,','), '', $v['goods_list_compositeAttrId']);
					$data[$k]['goods_list_compositeAttrId'] = $v['goods_list_compositeAttrId'];
				}
			}
//			if(count($attrInfo)>=1){
//				$attrInfo['error'] = 0;
//			}else{
//				$attrInfo['error'] = 1;
//			}
			$this->ajax($data);
		}		
	}
	
	//加入购物车 异步
	public function addCar(){
		
	}
	
	//收藏商品
	public function collect(){
		if(IS_AJAX){
			$uid = $_SESSION['uid'];
			$gid = Q('post.gid');
			$userInfo = M("user")->where("uid ='{$uid}'")->find();
			//判断用户是否有收藏
			if(!empty($userInfo['collect']) || $userInfo['collect'] != '' ){
				//判断商品gid是否重复收藏
				if(strstr(trim($userInfo['collect']),$gid)){
					$data['error'] = 0;//商品已收藏
					$this->ajax($data);
					return false;
				}else{
					$data['collect']= $userInfo['collect'] .',' . $gid;
				}				
			}else if($userInfo['collect'] == ''){
				$data['collect']= $gid;
			}
			M("user")->where("uid ='{$uid}'")->update($data);
			$data['error'] = 1;//收藏成功状态
			$this->ajax($data);
		}
	}
	
	//加入购物车
	public function addGoodsCar(){
		if(IS_AJAX){
			$data = $_POST;
			if(isset($_POST['color']) || isset($_POST['size']) ){
			$data['options']= array(  // 其他参数如价格、颜色、可以为数组或字符串
					'color' => $_POST['color'],
					'size' =>  $_POST['size'],
					'img'  =>  $_POST['imgUrl']
					 );
				$data['img'] = $_POST['imgUrl'];
			}

			cart::add($data);
			$this->ajax(1);
		}
	}
	
	//用户咨询
	public function question(){
		if(IS_AJAX){
			    $data['goods_gid'] =Q('post.goods_gid');
			    $data['question_question_uid'] =Q('post.question_question_uid');
			    $data['question_con'] = Q('post.question_con');
			    $data['question_type'] = Q('post.question_type');
				$data['question_addtime'] = time();
			M('question')->add($data);
			$this->ajax(1);
		}
		//实力化验证码
	}
	

}
